﻿
#include <windows.h>
#include <windowsx.h>
#include <atlstr.h>// CString的头文件  //
#include "resource.h"
#include "DialogMain.h"
#include<math.h>
BOOL A_Op= FALSE;
//主窗口信息处理函数//
//WM_COMMAND - 处理应用程序菜单;WM_PAINT - 绘制主窗口;WM_DESTORY - 发送退出信息并返回//
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		/* BEGIN MESSAGE CRACK */
		HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
		HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
		HANDLE_MSG(hWnd, WM_CLOSE, Main_OnClose);
		/* END MESSAGE CRACK */
	}

	return FALSE;
}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
	/* 设置程序图标 */
	HICON hIcon;
	hIcon = hIcon = LoadIcon((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), MAKEINTRESOURCE(1));
	SendMessage(hwnd, WM_SETICON, TRUE, (LPARAM)hIcon);//大图标//
	SendMessage(hwnd, WM_SETICON, FALSE, (LPARAM)hIcon);//小图标//
	return TRUE;
}
/*void TrimNumber(TCHAR a[])
{
	for (unsigned i = 0; i < lstrlen(a); i++)
	{
		if (a[i] == '.')
		{
			for (unsigned j = lstrlen(a) - 1; j >= 1; j--)
			{
				if (a[j] = '0')
				{
					a[j] = '\0';
				}
				else if (a[j] == '.')
				{
					a[j] = '\0';
				}
				else break;
			}
     	}
	}
}*/

double Operate(char Operator, double n1, double n2)
{
	if (Operator == '0')
	{
	}
	if (Operator == '+')
	{
		n2 += n1;
	}
	if (Operator == '-')
	{
		n2 = n1 - n2;
	}
	if (Operator == '*')
	{
		n2 *= n1;
	}
	if (Operator == '/')
	{
		n2 = n1 / n2;
	}
	if (Operator == '^')
	{
		n2 = pow(n1, n2);
	}
	return n2;
}
void IntBinary(TCHAR a[], int n)
{
	if (n > 1)
		IntBinary(a, n/2);
	wsprintf(a, L"%s%i",a,n%2);
}
void decimal(TCHAR a[], double m)
{
	if (m > 0.000001)
	{
		m = m * 2;
		wsprintf(a, L"%s%d", a, (long)m);
		decimal(a, m - (long)m);
	}
}
void Binary(TCHAR a[], double Num)
{
	TCHAR DecP[256];
	double x, y;
	double *iptr = &y;
	x = modf(Num, iptr);
	decimal(DecP, x);
	IntBinary(a, (int)y);
	lstrcat(a, L".");
	lstrcat(a, DecP);
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
	static  int DELTIMES = 0;
	static TCHAR Operator = '0';
	static TCHAR str1[256];
	static TCHAR num[3];
	switch (id)
	{
	case IDC_BUTTON1://数字1
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("1"));
		lstrcat(str1,TEXT("1"));
		SetDlgItemText(hwnd, IDC_EDIT1,str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON2://数字2
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("2"));
		lstrcat(str1, TEXT("2"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON3://数字3
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("3"));
		lstrcat(str1, TEXT("3"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON4://数字4
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("4"));
		lstrcat(str1, TEXT("4"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON5://数字5
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("5"));
		lstrcat(str1, TEXT("5"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON6://数字6
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("6"));
		lstrcat(str1, TEXT("6"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON7://数字7
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("7"));
		lstrcat(str1, TEXT("7"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON8://数字8
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("8"));
		lstrcat(str1, TEXT("8"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON9://数字9
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("9"));
		lstrcat(str1, TEXT("9"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_BUTTON10://数字0
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("0"));
		lstrcat(str1, TEXT("0"));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;

	
	case IDC_BUTTON11://小数点”.“
	{
		if (A_Op)
		{
			SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		}
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		//_tcscat(str1, TEXT("."));
		lstrcat(str1, TEXT("."));
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		num[1] = _ttof(str1);
		A_Op = FALSE;
	}
		break;
	case IDC_JIA://加法
	{
		num[1] = _ttof(str1);
		num[0] = num[1];
		num[1] = num[2];
		num[2] = Operate(Operator, num[1], num[0]);
		_itow(num[2], str1, 10);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		//TrimNumber(str1);
		Operator = '+';
		DELTIMES = 0;
		A_Op = TRUE;
	}
		break;
	case IDC_JIAN://减法
	{
		num[1] = _ttof(str1);
		num[0] = num[1];
		num[1] = num[2];
		num[2] = Operate(Operator, num[1], num[0]);
		_itow(num[2], str1, 10);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		//TrimNumber(str1);
		Operator = '-';
		DELTIMES = 0;
		A_Op = TRUE;
	}
		break;
	case IDC_CHENG://乘法
	{
		num[1] = _ttof(str1);
		num[0] = num[1];
		num[1] = num[2];
		num[2] = Operate(Operator, num[1], num[0]);
		_itow(num[2], str1, 10);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		//TrimNumber(str1);
		Operator = '*';
		DELTIMES = 0;
		A_Op = TRUE;
	}
		break;
	case IDC_CHU://除法
	{
		num[1] = _ttof(str1);
		num[0] = num[1];
		num[1] = num[2];
		num[2] = Operate(Operator, num[1], num[0]);
		_itow(num[2], str1, 10);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		//TrimNumber(str1);
		Operator = '/';
		DELTIMES = 0;
		A_Op = TRUE;
	}
		break;
	case IDC_PINGFAGNGEN://开根号
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = sqrt((double)_ttof(str1));
		_itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}
		break;
	case IDC_SIN://SIN
	{//SIN
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = sin((double)_ttof(str1));
		_itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}break;
	
	case IDC_COS://COS
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = cos((double)_ttof(str1));
		_itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}break;
	case IDC_TAN://TAN
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = cos((double)_ttof(str1));
		_itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}break;

	case IDC_BAI://e的x次方
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = exp((double)_ttof(str1));
		_itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}break;
	case IDC_FENG://log10
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = log10((double)_ttof(str1));
		_itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}break;
	case IDC_MI://X的Y次方
	{
		num[1] = _ttof(str1);
		num[0] = num[1];
		num[1] = num[2];
		num[2] = Operate(Operator, num[1], num[0]);
		_itow(num[2], str1, 10);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		//TrimNumber(str1);
		Operator = '^';
		DELTIMES = 0;
		A_Op = TRUE;
	}
		break;
	case IDC_CLEAR://清除数据
	{
		num[0] = num[1] = num[2];
		memset(str1, 0, sizeof(str1));
		SetDlgItemText(hwnd, IDC_EDIT1, NULL);
		Operator = '0';
		DELTIMES = 0;
		A_Op = FALSE;
		
	}
		break;
	case IDC_ER://二进制
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = _ttof(str1);
		_itow(num[2], str1, 2);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}
		break;
	case IDC_BA://八进制
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = _ttof(str1);
		_itow(num[2], str1, 8);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}
		break;
	case IDC_S://十进制
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = _ttof(str1);
		_itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}
		break;
	case IDC_SHILIU://十六进制
	{
		GetDlgItemText(hwnd, IDC_EDIT1, str1, sizeof(str1));
		num[2] = _ttof(str1);
		_itow(num[2], str1, 16);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		A_Op = TRUE;
	}
		break;
	case ID_OK:
	{
		GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
		num[1] = _ttof(str1);
		num[0] = num[1];
		num[1] = num[2];
		num[2] = Operate(Operator, num[1], num[0]);
        _itow(num[2], str1, 10);
		//TrimNumber(str1);
		SetDlgItemText(hwnd, IDC_EDIT1, str1);
		Operator = '0';
		DELTIMES = 0;
	}
	default:
		break;
	}
}

void Main_OnClose(HWND hwnd)
{
	int exit = MessageBox(hwnd, TEXT("退出计算器？"), TEXT("关闭 "), MB_YESNO | MB_ICONASTERISK);
	switch (exit)
	{
	case IDYES:
		EndDialog(hwnd, 0);
		break;
	case IDNO:
		break;
	default:
		break;
	}
}